/**   
 * @Title: ShiyixuanwuActGroup.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu 
 * @Description: 十一选五Activity，十一选五的入口
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-6-25 下午04:03:13  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu;

import android.app.ActivityGroup;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import android.widget.TextView;

import com.esun.config.Config;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.HelpAct;

import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.rabbittrunk.ShiyixuanwuBuyingInfo;

import com.esun.rabbit2.viewer.mainact.lotteryinfoact.LatestdetailsAct;
import com.esun.rabbit2.viewer.mainact.lotteryinfoact.LotteryInfoListAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord.LotteryRecordShiYiXuanWuAct;

import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: ShiyixuanwuActGroup
 * @Description: 十一选五Activity
 * @Author: Miko baoz@500wan.com
 * @Date: 2011-6-25 下午04:03:13
 */
public class ShiyixuanwuActGroup extends ActivityGroup implements OnClickListener {

	private View btnShiyixuanwuGoback; // 返回按钮
	private View btnShiyixuanwuMenu; // 菜单按钮

	private Dialog menuDialog; // 弹出菜单

	private LinearLayout linearMenuBg;
	private TextView tvTitle;

	private Bundle bundle;
	private boolean isRebuy;
	/********** 1.6.0kael修改 **************************/
	/** 承载最新详情和更多期次的按钮的LinearLayout */
	private LinearLayout lltitle;
	/** 最新详情按钮 */
	private LinearLayout lllatestdetails;
	/** 更多期次按钮 */
	private LinearLayout llmoretime;
	/** 最新详情图标 */
	private ImageView ivlatestdetails;
	/** 最新详情字样 */
	private TextView tvlatestdetails;
	/** 更多其次图标 */
	private ImageView ivmoretime;
	/** 更多期次字样 */
	private TextView tvmoretime;
	/** 动态加载Activity的ScrollView */
	private FrameLayout container;
	Bundle hemaiBundle;
	private Bundle lotteryinfoBundle;
	public static final int CHOOSE = 0;
	public static final int LOTTERYRECORD = 1;
	public static final int LOTTERYINFO = 2;
	public static final int INTRODUCE = 3;
	public static final int LATESTDETAILS = 4;
	public static final int MORETIME = 5;
	private int actid = CHOOSE;

	/************************************************/
	// 临时存放等City 放入通用字符串类中
	// private static final String CHOOSE_PAGE = "选号投注";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shiyixuanwu_activity_group);
		init();
		initPopMenu();
		initHandler();

		bundle = this.getIntent().getExtras();
		isRebuy = bundle.getBoolean(Config.IS_REBUY);
		try {
			actid = bundle.getInt(Config.CURRENT_TAB_NUM);
		} catch (Exception e) {
			actid = CHOOSE;
		}
		addViewActivity(actid);
	}

	private void init() {
		lotteryinfoBundle = new Bundle();
		lotteryinfoBundle.putString("lotid", Config.SHIYIXUANWU);
		lotteryinfoBundle.putString("type", "4");
		btnShiyixuanwuGoback = this.findViewById(R.id.shiyixuanwu_activity_group_btn_goBack);
		btnShiyixuanwuGoback.setOnClickListener(this);

		btnShiyixuanwuMenu = this.findViewById(R.id.shiyixuanwu_activity_group_btn_menu);
		btnShiyixuanwuMenu.setOnClickListener(this);

		tvTitle = (TextView) findViewById(R.id.shiyixuanwu_activity_group_tv_title);
		lltitle = (LinearLayout) findViewById(R.id.shiyixuanwuagp_webview_ll_title);
		lllatestdetails = (LinearLayout) findViewById(R.id.shiyixuanwuagp_ll_latestdetails);
		lllatestdetails.setOnClickListener(this);
		llmoretime = (LinearLayout) findViewById(R.id.shiyixuanwuagp_ll_moretime);
		llmoretime.setOnClickListener(this);
		ivlatestdetails = (ImageView) findViewById(R.id.shiyixuanwuagp_iv_latestdetails);
		ivmoretime = (ImageView) findViewById(R.id.shiyixuanwuagp_iv_moretime);
		tvlatestdetails = (TextView) findViewById(R.id.shiyixuanwuagp_tv_latestdetails);
		tvmoretime = (TextView) findViewById(R.id.shiyixuanwuagp_tv_moretime);
		container = (FrameLayout) findViewById(R.id.shiyixuanwuagp_containerBody);

		if (ShiyixuanwuChooseOkList.getChooseList().getSize() > 0) {
			tvTitle.setText("11选5投注列表");
		} else {
			tvTitle.setText("11选5投注");
		}

		if (isRebuy) {
			btnShiyixuanwuGoback.setVisibility(View.INVISIBLE);
			((ImageButton) btnShiyixuanwuMenu).setImageResource(R.drawable.cancel_rebuy_25x25);
		}
	}

	private void initPopMenu() {
		menuDialog = new Dialog(this, R.style.dialogNoFloating);
		Window dialogWindow = menuDialog.getWindow();
		menuDialog.getWindow().setContentView(R.layout.shiyixuanwu_menu_dialog);

		View.OnClickListener menuClickListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				menuDialog.dismiss();

				switch (v.getId()) {

				// 选号投注
				case R.id.shiyixuanwu_menu_dialog_btn_choose:

					actid = CHOOSE;
					addViewActivity(actid);

					break;

				// 购彩记录
				case R.id.shiyixuanwu_menu_dialog_btn__lotteryRecord:

					actid = LOTTERYRECORD;
					addViewActivity(actid);

					break;

				// 追号记录
				// case R.id.shiyixuanwu_menu_dialog_btn_followRecord:
				// break;

				// 开奖记录
				case R.id.shiyixuanwu_menu_dialog_btn_lotteryInfo:
					actid = LATESTDETAILS;
					addViewActivity(actid);
					break;

				// 玩法介绍
				case R.id.shiyixuanwu_menu_dialog_btn_introduce:
					actid = INTRODUCE;
					addViewActivity(actid);
					break;

				// 关闭
				// case R.id.shiyixuanwu_menu_dialog_btn_close:
				// break;

				case R.id.shiyixuanwu_menu_dialog_linear_bg:
					break;

				default:
					break;
				}
			}
		};

		// 选号投注
		Button btnChoose = (Button) dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_btn_choose);
		btnChoose.setOnClickListener(menuClickListener);

		// 购彩记录
		Button btnRecord = (Button) dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_btn__lotteryRecord);
		btnRecord.setOnClickListener(menuClickListener);

		// 追号记录
		// Button btnFollow = (Button)
		// dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_btn_followRecord);
		// btnFollow.setOnClickListener(menuClickListener);

		// 开奖信息
		Button btnLotteryInfo = (Button) dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_btn_lotteryInfo);
		btnLotteryInfo.setOnClickListener(menuClickListener);

		// 玩法介绍
		Button btnIntorduce = (Button) dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_btn_introduce);
		btnIntorduce.setOnClickListener(menuClickListener);

		// 关闭
		// Button btnClose = (Button)
		// dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_btn_close);
		// btnClose.setOnClickListener(menuClickListener);

		linearMenuBg = (LinearLayout) dialogWindow.findViewById(R.id.shiyixuanwu_menu_dialog_linear_bg);
		linearMenuBg.setOnClickListener(menuClickListener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		// 后退
		case R.id.shiyixuanwu_activity_group_btn_goBack:
			ShiyixuanwuBuyingInfo.getBuyingInfo().replaceAllData();
			this.finish();
			break;

		// 菜单
		case R.id.shiyixuanwu_activity_group_btn_menu:
			if (UserInfo.getUserInfo().isRebuy()) {
				UserInfo.getUserInfo().setRebuy(false);
				ShiyixuanwuChooseOkList.getChooseList().reset();
				ShiyixuanwuActGroup.this.finish();
			} else {
				menuDialog.show();
			}
			break;
		case R.id.shiyixuanwuagp_ll_latestdetails:
			
				actid = LATESTDETAILS;
				addViewActivity(actid);
			
			break;

		case R.id.shiyixuanwuagp_ll_moretime:
			
				actid = MORETIME;
				addViewActivity(actid);

			
			break;
		default:
			EDebug.o(this, "ShiyixuanwuActGroup switch ERROR!! ");
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		EDebug.o(this, "onKeyDown ==> " + event);

		if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
			menuDialog.show();
			return true;
		}

		return super.onKeyDown(keyCode, event);

	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {

		return true;
	}

	@Override
	protected void onPause() {

		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {

		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

	public static Handler handlerShiyixuanwuActGroup;
	public static final int IN_CHOOSE_VIEW = 100021;
	public static final int IN_LIST_VIEW = 100022;

	private void initHandler() {
		handlerShiyixuanwuActGroup = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				EDebug.o(ShiyixuanwuActGroup.this, "handler star");
				switch (msg.what) {
				case IN_CHOOSE_VIEW:
					tvTitle.setText("11选5投注");
					break;

				case IN_LIST_VIEW:
					tvTitle.setText("11选5投注列表");
					break;

				default:
					break;
				}
			}

		};
	}

	public void addViewActivity(int viewId) {
		switch (viewId) {
		case CHOOSE:
			if (ShiyixuanwuChooseOkList.getChooseList().getSize() > 0) {
				tvTitle.setText("11选5投注列表");
			} else {
				tvTitle.setText("11选5投注");
			}
			lltitle.setVisibility(View.GONE);
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("choose", new Intent(ShiyixuanwuActGroup.this, ShiyixuanwuChoosePageAct.class)).getDecorView());
			Message msg = ShiyixuanwuChoosePageAct.handler.obtainMessage(ShiyixuanwuChoosePageAct.DO_REFRESH, null);
			ShiyixuanwuChoosePageAct.handler.sendMessage(msg);
			break;
		case LOTTERYRECORD:
			if (UserInfo.getUserInfo().getUserSession().length() > 3) {
				lltitle.setVisibility(View.GONE);
				tvTitle.setText("11选5购彩记录");
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity("record", new Intent(ShiyixuanwuActGroup.this, LotteryRecordShiYiXuanWuAct.class)).getDecorView());
				Message msgRefresh = new Message();
				msgRefresh.what = Config.DO_REFRESH;
				LotteryRecordShiYiXuanWuAct.handlerGetShiYiXuanWuLotteryRecord.sendMessage(msgRefresh);
			} else {
				startActivity(new Intent().setClass(ShiyixuanwuActGroup.this, LoginAct.class));
			}

			break;
		
		case INTRODUCE:
			lltitle.setVisibility(View.GONE);
			tvTitle.setText("11选5玩法介绍");
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("introduction",
					new Intent(ShiyixuanwuActGroup.this, HelpAct.class).putExtra("url", Config.SHIYIXUANWU_INTRODUCE_WEB_ADD).putExtra("title", Config.NULLMARK).putExtra("havetitle", Config.NUM0))
					.getDecorView());
			break;
		case LATESTDETAILS:
			lltitle.setVisibility(View.VISIBLE);
			lllatestdetails.setBackgroundResource(R.drawable.anull_icon);
			llmoretime.setBackgroundResource(R.drawable.btn_default_title_right_88x30);
			ivlatestdetails.setImageResource(R.drawable.icon_pressed_this_13x23);
			ivmoretime.setImageResource(R.drawable.icon_default_all_24x24);
			tvlatestdetails.setTextColor(getResources().getColor(R.color.white));
			tvmoretime.setTextColor(getResources().getColor(R.color.lotteryinfodefault));
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("lotteryinfo", new Intent(ShiyixuanwuActGroup.this, LatestdetailsAct.class).putExtras(lotteryinfoBundle)).getDecorView());
			break;
		case MORETIME:
			lltitle.setVisibility(View.VISIBLE);
			lllatestdetails.setBackgroundResource(R.drawable.btn_default_title_left_88x30);
			llmoretime.setBackgroundResource(R.drawable.anull_icon);
			ivlatestdetails.setImageResource(R.drawable.icon_default_this_13x23);
			ivmoretime.setImageResource(R.drawable.icon_pressed_all_24x24);
			tvlatestdetails.setTextColor(getResources().getColor(R.color.lotteryinfodefault));
			tvmoretime.setTextColor(getResources().getColor(R.color.white));
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("moretime", new Intent(ShiyixuanwuActGroup.this, LotteryInfoListAct.class).putExtra("lotid", Config.SHIYIXUANWU)).getDecorView());
			break;
		}
	}

}
